;
layui.extend({
	treetable: 'res/js/component/treetable',
}).define(['form', 'treetable'],
function(exports) {
	var $ = layui.$,
	form = layui.form,
	treetable = layui.treetable,
	layer = layui.layer,
	table = layui.table;
	var cols = [[{
		type: 'numbers',
		title: "序号",
		width: 50
	},
	{
		field: 'name',
		title: '资源名称',
		minWidth: 300
	},
	{
		field: 'permission',
		title: '权限码',
		minWidth: 100
	},
	{
		field: 'icon',
		title: '图标',
		minWidth: 120
	},
	{
		field: 'url',
		title: '访问路径',
		minWidth: 250
	},
	{
		field: 'priority',
		title: '显示顺序',
		minWidth: 30
	},
	{
		field: 'type',
		title: '资源类型',
		minWidth: 50,
		templet: function(res) {
			if (res.type == 1) {
				return '<button class="layui-btn layui-btn-sm layui-bg-red">系统</button>';
			} else if (res.type == 2) {
				return '<button class="layui-btn layui-btn-sm layui-bg-blue">目录</button>';
			} else if (res.type == 3) {
				return '<button class="layui-btn layui-btn-sm layui-bg-green">菜单</button>';
			} else if (res.type == 4) {
				return '<button class="layui-btn layui-btn-sm layui-bg-gray">功能</button>';
			} else {
				return '<button class="layui-btn layui-btn-sm layui-bg-black">根节点</button>';
			}
		}
	},
	{
		field: 'available',
		title: '状态',
		minWidth: 110,
		unresize: true,
		templet: function(res) {
			if (res.available == 1) {
				return '<input type="checkbox" name="resource_lock" value="' + res.id + '" title="锁定" lay-filter="resourceLock">';
			} else {
				return '<input type="checkbox" name="resource_lock" value="' + res.id + '" title="锁定" lay-filter="resourceLock" checked >';
			}
		}
	},
	{
		title: "操作",
		width: 170,
		align: 'center',
		templet: function(res) {
			var result = "";
			if(res.available == 1){
                if (layui.data('auth').authList.includes("resource:add")) {
                    result += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</a>';
                }
                if (layui.data('auth').authList.includes("resource:edit")) {
                    result += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>';
                }
			}
			return result;
		}
	}]];
	//权限判断
	if (!layui.data('auth').authList.includes("resource:add") && !layui.data('auth').authList.includes("resource:edit")) {
		cols[0].pop();
	}
	//表格渲染
	var renderTable = function() {
		treetable.render({
			elem: '#resource-tree-table',
			url: '/admin/resource/inquiry/loop',
			method: 'post',
			limit: 1000,
			treeColIndex: 1,
			treeSpid: -1,
			treeIdName: 'id',
			treePidName: 'parentId',
			toolbar: '#resource-toolbar',
			defaultToolbar: ['filter'],
			title: '资源数据表',
			height: 'full-60',
			loading: true,
			cols: cols,
			done: function() {
				layer.closeAll('loading');
				//这里可以用来设置disabled
                if (!layui.data('auth').authList.includes('resource:status')) {
                    $("input[name='resource_lock']").attr("disabled",true);
                }
			}
		});
	}
	renderTable();
	table.on('toolbar(resource-tree-table)',
	function(obj) {
		switch (obj.event) {
		case 'expand':
			treetable.expandAll('#resource-tree-table');
			break;
		case 'fold':
			treetable.foldAll('#resource-tree-table');
			break;
		case 'refresh':
			renderTable();
			break;
		};
	});
	table.on('tool(resource-tree-table)',
	function(obj) {
		var data = obj.data;
		if (obj.event === 'del') {
			layer.confirm('真的删除行么',
			function(index) {
				layer.close(index);
			});
		} else if (obj.event === 'add') {
			layer.open({
				type: 2,
				title: '添加资源信息',
				content: '/resource/add/html',
				maxmin: true,
				area: ['450px', '480px'],
				btn: ['确定', '取消'],
				yes: function(index, layero) {
					var iframeWindow = window['layui-layer-iframe' + index],
					submitID = 'resource-add-submit',
					submit = layero.find('iframe').contents().find('#' + submitID);
					var parentId = data.id;
					//监听提交
					iframeWindow.layui.form.on('submit(' + submitID + ')',
					function(data) {
						var field = data.field; //获取提交的字段
						field.parentId = parentId;
						var userInfo = layui.data('admin').userInfo;
						$.extend(field, {
							'createOperator': userInfo.username
						});
						var jsonData = JSON.stringify(field);
						//提交 Ajax 成功后，静态更新表格中的数据
						$.ajax({
							url: '/admin/resource/addition',
							type: 'post',
							contentType: "application/json",
							dataType: 'json',
							data: jsonData,
							success: function(data) {
								if (data.transactionStatus.success) {
									layer.msg("添加成功");
									renderTable(); //数据刷新
									layer.close(index); //关闭弹层
								} else {
									layer.msg(data.transactionStatus.replyText);
								}
							}
						});
					});
					submit.trigger('click');
				}
			});
		} else if (obj.event === 'edit') {
			layer.open({
				type: 2,
				title: '修改资源信息',
				content: '/resource/edit/html',
				maxmin: true,
				area: ['450px', '480px'],
				btn: ['确定', '取消'],
				yes: function(index, layero) {
					var iframeWindow = window['layui-layer-iframe' + index],
					submitID = 'resource-edit-submit',
					submit = layero.find('iframe').contents().find('#' + submitID);
					var resourceId = data.id;
					//监听提交
					iframeWindow.layui.form.on('submit(' + submitID + ')',
					function(data) {
						var field = data.field; //获取提交的字段
						field.id = resourceId;
						var userInfo = layui.data('admin').userInfo;
						$.extend(field, {
							'modifyOperator': userInfo.username
						});
						var jsonData = JSON.stringify(field);
						//提交 Ajax 成功后，静态更新表格中的数据
						$.ajax({
							url: '/admin/resource/modify',
							type: 'post',
							contentType: "application/json",
							dataType: 'json',
							data: jsonData,
							success: function(data) {
								if (data.transactionStatus.success) {
									layer.msg("修改成功");
									renderTable(); //数据刷新
									layer.close(index); //关闭弹层
								} else {
									layer.msg(data.transactionStatus.replyText);
								}
							}
						});
					});
					submit.trigger('click');
				},
				success: function(layero, index) {
					var jsonData = JSON.stringify({
						'id': data.id
					});
					$.ajax({
						url: '/admin/resource/modify/inquiry',
						type: 'post',
						contentType: "application/json",
						dataType: 'json',
						data: jsonData,
						success: function(data) {
							if (data.transactionStatus.success) {
								var iframeWindow = window['layui-layer-iframe' + index];
								iframeWindow.initData(data);
							} else {
								layer.msg(data.transactionStatus.replyText);
							}
						}
					});
				}
			});
		}
	});
	form.on('checkbox(resourceLock)',
	function(obj) {
		var id = this.value;
		var status;
		if (obj.elem.checked) {
			status = 0;
		} else {
			status = 1;
		}
		var userInfo = layui.data('admin').userInfo;
		var jsonData = JSON.stringify({
			"id": id,
			"available": status,
			'modifyOperator': userInfo.username
		});
		$.ajax({
			url: '/admin/resource/modify/status',
			type: 'post',
			contentType: "application/json",
			dataType: 'json',
			data: jsonData,
			success: function(data) {
				if (data.transactionStatus.success) {
					layer.msg("修改成功");
					renderTable(); //数据刷新
				} else {
					layer.msg(data.transactionStatus.replyText);
				}
			}
		});
	});
	exports('resourceList',
	function() {});
});